<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use App\Models\BbsAnswerComment;

class BbsAnswerCommentController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!$this->user->can('viewBbsComment')) abort(405);
        
        $query_arr = array_except(Input::all(), ['_token']);
        $bbs_ques = new BbsAnswerComment();
        if (isset($query_arr['keywords']) AND $query_arr['keywords'] != '')
            $bbs_ques = $bbs_ques->where('message', 'like', "%" . $query_arr['keywords'] . "%");
        $comments = $bbs_ques->paginate(20);
        foreach ($comments as $key => $val) {
            $comments[$key]['answer_content'] = $val->getAnswerByComment['answer_content'];
        }
        return view('admin.bbs.comments.index')
            ->with('comments', $comments);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy(Request $request)
    {
        if(!$this->user->can('deleteBbsComment'))
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        
        $id = $request->input('id');
        $bbs_comment = BbsAnswerComment::find($id);
        if ($bbs_comment->delete())
            return  Response::json(array('status'=>'success','msg'=>'评论 [' . str_limit($bbs_comment->message, 15, '...') . '] 删除成功'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function deletes(Request $request)
    {
        if(!$this->user->can('deleteBbsComment'))
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        //
        $ids = explode(',', $request->input('id'));
        $bbs_comment = BbsAnswerComment::whereIn('id', $ids);
        $count = $bbs_comment->count();
        if ($bbs_comment->delete())
            return  Response::json(array('status'=>'success','msg'=>'成功删除 [' . $count . '] 评论'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
